<template>
  <div>
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="close"
      :title="title"
      width="800px"
      :top="'10vh'"
    >
      <el-form ref="form" :model="form" size="small" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item
              label="所属分组"
              prop="group"
              :rules="[{ required: true, message: '请选择分组', trigger: 'change' }]"
            >
              <el-select v-model="form.group" clearable filterable placeholder="请选择分组">
                <el-option
                  v-for="obj in groupList"
                  :key="obj.id"
                  :label="obj.name"
                  :value="obj.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="服务"
              prop="service.id"
              :rules="[{ required: true, message: '请选择服务', trigger: 'change' }]"
            >
              <el-select v-model="form.service.id" clearable filterable placeholder="请选择服务">
                <el-option
                  v-for="obj in serviceList"
                  :key="obj.id"
                  :label="obj.name"
                  :value="obj.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="简介"
              prop="jj"
              :rules="[{ required: true, message: '请输入服务简介', trigger: 'blur' }]"
            >
              <el-input
                v-model="form.jj"
                type="textarea"
                placeholder="请输入简介"
                maxlength="150"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="排序"
              prop="px"
              style="width: 100%"
            >
              <el-input-number v-model="form.px" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="close">取消</el-button>
        <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { saveObject, updateObject } from '@/api/yslb/group'
import { queryAllService } from '@/api/system/serviceManager'
import { queryAll } from '@/api/yslb/index'
export default {
  name: 'RightEntity',
  data() {
    return {
      title: '数据项管理',
      loading: false,
      dialogFormVisible: false,
      groupList: [],
      serviceList: [],
      form: {
        id: null,
        group: null,
        service: {
          id: null
        },
        jj: '',
        px: 0
      },
      queryData: null
    }
  },
  methods: {
    openEntity(data, parent) {
      this.initService()
      this.initAllGroup()
      if (undefined !== parent && parent !== null && parent.id !== null) {
        this.form.group = parent.id
      }
      if (undefined !== data && data !== null && undefined !== data.id && data.id !== null) {
        this.form = Object.assign({}, data)
        this.title = '修改数据项信息'
        this.dialogFormVisible = true
      } else {
        this.title = '新增数据项信息'
        this.dialogFormVisible = true
      }
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    initService() {
      queryAllService().then(res => {
        const { data } = res
        this.serviceList = data
      })
    },
    initAllGroup() {
      queryAll().then(res => {
        const { data } = res
        this.groupList = data.data
      })
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    }
  }
}
</script>

<style scoped>

</style>
